package com.yingxue.lesson.controller;

import com.yingxue.lesson.constant.Constant;
import com.yingxue.lesson.exception.DataResult;
import com.yingxue.lesson.service.HomeService;
import com.yingxue.lesson.utils.JwtTokenUtil;
import com.yingxue.lesson.vo.resp.HomeRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author chahua
 * @date 2022/4/10 22:27
 **/
@RestController
@RequestMapping("/api")
@Api(tags = "首页模块",description = "首页相关模块")
public class HomeController {

    @Autowired
    private HomeService homeService;

    @GetMapping("/home")
    @ApiOperation(value = "获取首页数据接口")
    public DataResult<HomeRespVO> getHome(HttpServletRequest request){
        // 获取登录用户的accessToken，必须通过登录后才能访问该接口
        String accessToken=request.getHeader(Constant.ACCESS_TOKEN);
        // 根据请求token获取userId
        String userId= JwtTokenUtil.getUserId(accessToken);

        DataResult result =DataResult.success();
        HomeRespVO homeRespVO = homeService.getHome(userId);
        result.setData(homeRespVO);
        return result;
    }
}
